<!--
* description: 试驾车资料查看
* author: wangml
* createdDate: 2020-11-30
-->

<template>
  <div class="app-container app-container-table">
    <!-- 单表格模版页面 -->
    <one-table-template
      ref="multipleTable"
      :dynamic-api-config="dynamicApiConfig"
      :dynamic-buttons="dynamicButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="dynamicTableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-more-btn="true"
      :dynamic-is-show-select="true"
      :dynamic-api-style="apiStyle"
      :dynamic-page-size="15"
      :refresh-table-key="refreshTableKey"
      :callback-obj="callbackObj"
      @hiddenColData="hiddenColData"
    />
    <edit
      v-if="showdia"
      :dynamic-api-config="{APIUrl: 've/orderadd'}"
      dynamic-api-style="restful"
      :popups-visible.sync="showdia"
      :popups-state="popstate"
      :dynamic-text-map="{edit:'资料查看',add:'资料查看'}"
      :dynamic-edit-row-data="clickRowsData"
      @close="popClose"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import edit from './edit'
import Vue from 'vue'
import api from '@/api/testDriver'
import commData from '../common'
export default {
  name: 'TDInformationReview',
  components: {
    OneTableTemplate,
    edit
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData(
      // 同时加载当前页面和编辑框页面的配置
      [to.path, ''],
      function() {
        next()
      })
  },
  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      curPopupsVisible: false,
      genCodeFiled: null,
      selectList: [],
      clickRowsData: {},
      popstate: 'add',
      showdia: false,
      // 指定请求类型 可以统一在 utils/config.js中配置
      apiStyle: 'restful',
      dynamicApiConfig: api.findCarInfor,
      refreshTableKey: '0',
      ruleSettingShow: false,
      // table表格查询API配置对象
      // 动态组件-查询条件
      tableComponents:
          this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents.length > 0
            ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents
            : [...commData.frontFields(1), ...commData.ModelOptionsNotDisabled()],
      formField: null,
      // 动态组件-按钮
      dynamicButtons: this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons.length > 0
        ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons
        : [
          {
            compKey: 'btnKey1',
            type: 'primary',
            size: 'small',
            name: 'search',
            clickEvent: () => this.queryTable(),
            text: '查询'
          },
          {
            compKey: 'btnKey2',
            type: '',
            size: 'small',
            name: 'reset',
            clickEvent: () => this.reset(),
            text: '重置'
          }
        ],
      // 动态组件-表格网格列
      dynamicTableCols: this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableCols.length > 0
        ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableCols
        : [
          {
            label: '操作',
            minWidth: 180,
            fixed: true,
            isComponent: true,
            comps: [
              {
                compKey: 'comKey2',
                labelName: '查看资料',
                component: () => import('@/components/org/linkButton'),
                clickEvent: (index) => this.edit(index)
              }

            ]
          },
          {
            prop: 'releaseId',
            label: '主键ID',
            hidden: true,
            alwaysHidden: true
          },
          {
            prop: 'updateControlId',
            label: '并发控制ID',
            hidden: true,
            alwaysHidden: true
          },
          {
            prop: 'saleOrderCode',
            label: '订单编号',
            width: 200
          },
          {
            prop: 'insaleOrderCode',
            label: '试驾车单号',
            width: 120
          },
          {
            prop: 'orderStatusCode',
            label: '订单状态'
          },
          {
            prop: 'salePlanCode',
            label: '销售计划号'
          },
          {
            prop: 'vin',
            label: 'VIN码'
          },
          {
            prop: 'saleOrderDate',
            label: '下单日期',
            width: 140
          },
          {
            prop: 'crmPreDeliverDate',
            label: '预计交付日期',
            width: 140
          },
          {
            prop: 'cityName',
            label: '交付城市'
          },
          {
            prop: 'dlrShortNameTiaofu',
            label: '交付门店'
          },
          {
            prop: 'dlrShortNameTiyan',
            label: '体验中心'
          },
          {
            prop: 'carSeriesCn',
            label: '车系'
          },
          {
            prop: 'carConfigCn',
            label: '基础车型名称'
          },
          {
            prop: 'optionalPagName',
            label: '选装包'
          },
          {
            prop: 'carColorName',
            label: '车身颜色'
          },
          {
            prop: 'carIncolorName',
            label: '内饰颜色'
          },
          {
            prop: 'price',
            label: '价格'
          },
          {
            prop: 'invoiceCustName',
            label: '企业名称'
          },
          {
            prop: 'invoiceCustPhone',
            label: '联系电话',
            width: 120
          },
          {
            prop: 'invoiceCustCredCode',
            label: '社会性用代码',
            width: 120
          }

        ],
      // 单表格模版页面组件回调事件
      callbackObj: {
        // 当选择项发生变化时会触发该事件（多选时）
        onCheckSelectChange: (selection) => {
          this.selectList = selection
          this.handleSelectionChange(selection)
        },
        onBeforeQuery: (ops) => {
          // 不兼容字段 转换
          const ss = [
            ['dlrId', 'experienceDlrId']
          ]
          ss.forEach(arr => {
            ops.variables[arr[1]] = ops.variables[arr[0]]
            delete ops.variables[arr[0]]
          })
        }
      }
    }
  },
  created() {
    const viewPageCode = this.$route.path
    this.genCodeFiled = this.genUtil(this.tableComponents)
    const formField = this.genCodeFiled('codeField')
    this.formField = this.$utils.getFormField(this, formField, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
  },
  mounted() {
    // 列表查询
    this.queryTable(1)
  },
  methods: {
    edit(index) {
      this.showdia = true
      this.clickRowsData = this.$refs['multipleTable'].list[index]
    },
    // 自动生成codeField
    genUtil: (array) => (attr) => array.reduce((p, c) => { p[c[attr]] = ''; return p }, {}),
    // 保存 或新增 后会调用此方法
    popClose(type) {
      this.showdia = false
      if (type == 1) {
        this.queryTable(1)
      }
    },
    // 当选择项发生变化时会触发该事件
    handleSelectionChange(selection) {
      const btns = this.dynamicButtons.filter(o => o.disabled !== undefined)
      if (btns.length > 0) {
        const isCheck = selection && selection.length > 0
        btns.forEach((item) => {
          item.disabled = !isCheck
        })
      }
    },

    hiddenColData(data) {
      if (data && data.length > 0) {
        this.refreshTableKey += 1
        this.dynamicTableCols = data
      }
    },

    // 新增
    // add() {
    //   this.clickRowsData = {}
    //   this.popstate = 'add'
    //   this.showdia = true
    // },
    selectRowsData(rowdata) {

    },

    // 查看附件
    checkFile(index, type) {
      // location.href = process.env.VUE_APP_BASE_API + this.$refs['multipleTable'].list[index].downloadUrl
      window.open(this.$refs['multipleTable'].list[index][type])
    },

    // 删除按钮单条删除
    delOneData(row) {
      const data = [this.$refs['multipleTable'].list[row]]
      this.delData(data)
    },

    // 数据批量删除
    delSeleteData() {
      const data = this.$refs['multipleTable'].$refs['multipleTable'].selection
      this.delData(data)
    },

    // 删除
    delData(selectData) {
      const that = this
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // requestRestful({
        //   apiUrl: '',
        //   variables: {},
        //   method: 'POST', // 默认post
        //   contentType: '', // 默认 'application/json; charset=UTF-8'
        //   lock: '',
        //   urlType: 'GET' // 处理非json请求
        // })
      }).catch(() => {})
    }
  }
}
</script>

